//
//  SRSportPart+Cate.m
//  SportRing
//
//  Created by Apple on 14/10/29.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRSportPart+Cate.h"
#import "SRDay+Cate.h"
#import "SRMe+Cate.h"

@implementation SRSportPart (Cate)

+ (SRSportPart *)sportPartForDay:(SRDay *)day
{
    __block SRSportPart * sportPart = nil;
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         sportPart = [SRSportPart MR_createEntity];
         sportPart.day = day;
     }];
    return sportPart;
}

- (void)configAttributesWithBlock:(void(^)(SRSportPart * aSelf))block
{
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         block(self);
     }];
}

@end

@implementation SRSportPart (Find)

+ (NSArray *)findAllUnuploadSportParts:(SRMe *)me
{
    __block NSMutableArray * mArray = [@[] mutableCopy];
    [me.days enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        SRDay * day = obj;
        [day.sportParts enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
            SRSportPart * part = obj;
            if (part.isSynced.boolValue == NO) {
                [mArray addObject:part];
            }
        }];
    }];
    return mArray;
}

+ (NSArray *)findSportPartsForDay:(SRDay *)day
{
    return [[day.sportParts allObjects] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        SRSportPart * part1 = obj1;
        SRSportPart * part2 = obj2;
        return [part1.section compare:part2.section];
    }];
}

+ (SRSportPart *)findSportPartForDay:(SRDay *)day forSection:(NSInteger)section
{
    __block SRSportPart * part = nil;
    [day.sportParts enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        SRSportPart * temp = obj;
        if (temp.section.intValue == section) {
            part = temp;
            *stop = YES;
        }
    }];
    return part;
}

+ (SRSportPart *)findSportPartCINEForDay:(SRDay *)day forSection:(NSInteger)section
{
    SRSportPart * sportPart = [SRSportPart findSportPartForDay:day forSection:section];
    if (sportPart == nil) {
        sportPart = [SRSportPart sportPartForDay:day];
        [sportPart configAttributesWithBlock:^(SRSportPart *aSelf) {
            aSelf.section = @(section);
        }];
    }
    return sportPart;
}

+ (float)totalMileage:(SRMe *)me
{
    NSPredicate * predicate = [NSPredicate predicateWithFormat:@""];
    
    [SRSportPart MR_findAllWithPredicate:predicate];
    
    return 0;
}

+ (float)totalCalorie:(SRMe *)me
{
    return 0;
}

+ (float)totalRunCounter:(SRMe *)me
{
    return 0;
}

+ (float)totalUpstairsCounter:(SRMe *)me
{
    return 0;
}

+ (float)totalWalkCounter:(SRMe *)me
{
    return 0;
}

@end

@implementation SRSportPart (Delete)

+ (void)deleteSportPart:(SRSportPart *)part
{
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         [part MR_deleteEntity];
     }];
}

@end
